package utils.validcode;
/**手机号正则:"^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$");
 * 网址正则: "((http|https):\\/\\/){1}[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?"
 * 预先定义,方便以后使用        手机号:
 */

import lombok.Data;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 手机号、邮箱、密码验证
 * @author Seven
 * @date 2021/11/17
 */
@Data
public class DataValid {

    private static String PATTEN_REGEX_PHONE= "^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$";

    private static String PATTEN_REGEX_URL= "((http|https):\\/\\/){1}[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?";

    private static String PATTEN_REGEX_EMAIL= "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";

    private static String PATTEN_SQL_IN="(.*\\=.*\\-\\-.*)|(.*(\\+|\\-).*)|(.*\\w+(%|\\$|#|&)\\w+.*)|(.*\\|\\|.*)|(.*\\s+(and|or)\\s+.*)|(.*\\b(select|update|union|and|or|delete|insert|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b.*)";
    /**
     * 密码验证：8位以上，字母大小写+数字
     */
    private static String PATTEN_REGEX_PWD = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$";

    /**
     * 是否含有sql注入，返回true表示含有
     * @param obj
     * @return
     */
    public static boolean containsSqlInjection(Object obj){
            Pattern pattern= Pattern.compile("\\b(and|exec|insert|select|drop|grant|alter|delete|update|count|chr|mid|master|truncate|char|declare|or)\\b|(\\*|;|\\+|'|%)");
        Matcher matcher=pattern.matcher(obj.toString().toLowerCase());
        return matcher.find();
    }

    public static boolean telValid(String tel){
        if (!tel.matches(PATTEN_REGEX_PHONE)) {
            return false;
        }
        return true;
    }

    public static boolean urlValid(String url){
        if(url.matches(PATTEN_REGEX_URL)){
            return false;
        }
        return true;
    }

    public static boolean emailValid(String email){
        if(!email.matches(PATTEN_REGEX_EMAIL)){
            return false;
        }
        return true;
    }

    public static boolean pwdValid(String pwd){
        if(!pwd.matches(PATTEN_REGEX_PWD)){
            return false;
        }
        return true;
    }

}
